<?php
defined('IN_IA') or exit('Access Denied');
require_once IA_ROOT . '/addons/xn_erp/inc/function.php';
global $_GPC, $_W;
$op = $_GPC['op'] ? "&op=" . $_GPC['op'] : '';
$url = $this->createWebUrl('set') . $op;

if ($_GPC['op'] == 'unit') {
    $title = "单位设置";
    if ($_W["isajax"]) {
        if ($_GPC['api'] == 'unit_list') {
            $unit = pdo_getslice('xn_erp_unit', array(), array($_GPC['page'], $_GPC['limit']), $count, array(), "", "id DESC");
            $unit_data = array("code" => 0, "msg" => "", "count" => $count, "data" => $unit);
            $unit_json = json_encode($unit_data);
            echo $unit_json;
            exit;
        }
        if ($_GPC['api'] == 'unit_del' && $_GPC['uid']) {
            $result = pdo_delete('xn_erp_unit', array('id' => $_GPC['uid']));
            if (!empty($result)) {
                echo "删除成功";
            } else {
                echo "删除失败";
            }
            exit;
        }
        if ($_GPC['api'] == 'unit_add' && $_GPC['name']) {
            $result = pdo_insert('xn_erp_unit', array('name' => $_GPC['name']));
            if (!empty($result)) {
                echo "增加成功";
            } else {
                echo "增加失败";
            }

            exit;
        }
        if ($_GPC['api'] == 'unit_edit' && $_GPC['name'] && $_GPC['uid']) {
            $result = pdo_update('xn_erp_unit', array('name' => $_GPC['name']), array('id' => $_GPC['uid']));
            if (!empty($result)) {
                echo "修改成功";
            } else {
                echo "修改失败";
            }

            exit;
        }
    }
    include $this->template('web/set/set_unit');
}

if ($_GPC['op'] == 'storage') {
    $title = "仓库设置";
    if ($_W["isajax"]) {
        if ($_GPC['api'] == 'list') {
            $storage = pdo_getslice('xn_erp_storage', array(), array($_GPC['page'], $_GPC['limit']), $count, array(), "", "id DESC");
            $storage_data = array("code" => 0, "msg" => "", "count" => $count, "data" => $storage);
            $storage_json = json_encode($storage_data);
            echo $storage_json;
            exit;
        }
        if ($_GPC['api'] == 'del' && $_GPC['sid']) {
            $result = pdo_delete('xn_erp_storage', array('id' => $_GPC['sid']));
            if (!empty($result)) {
                echo "删除成功";
            } else {
                echo "删除失败";
            }
            exit;
        }
        if ($_GPC['api'] == 'add' && $_GPC['sname']) {
            $result = pdo_insert('xn_erp_storage', array('sname' => $_GPC['sname'],'snumber'=>$_GPC['snumber'],'address'=>$_GPC['address'],'phone'=>$_GPC['phone']));
            if (!empty($result)) {
                echo "增加成功";
            } else {
                echo "增加失败";
            }

            exit;
        }
        if ($_GPC['api'] == 'edit' && $_GPC['sname'] && $_GPC['sid']) {
            $result = pdo_update('xn_erp_storage', array('sname' => $_GPC['sname'],'snumber'=>$_GPC['snumber'],'address'=>$_GPC['address'],'phone'=>$_GPC['phone']), array('id' => $_GPC['sid']));
            if (!empty($result)) {
                echo "修改成功";
            } else {
                echo "修改失败";
            }

            exit;
        }
    }
    include $this->template('web/set/set_storage');
}

if ($_GPC['op'] == 'category') {

    $where = "1";
    if ($_GPC['type'] == 'goods') {
        $title = "商品分类";
        $type = 1;
    }
    if ($_GPC['type'] == 'contact') {
        $title = "客户分类";
        $type = 2;
    }
    $cid = pdo_getcolumn('xn_erp_category', "1 ORDER by id DESC", 'id');
    $cid = $cid ? $cid : 0;

    if ($_W["isajax"]) {
        if ($_GPC['api'] == 'list') {
            $_GPC['limit'] = $_GPC['limit'] ? $_GPC['limit'] : 10;
            $category = pdo_getslice('xn_erp_category', array('type' => $type), array(), $count, array(), "", "sort ASC");
            foreach ($category as $key => $value) {
                $childid = unserialize($value['childid']);
                if (is_array($childid)) {
                    $category[$key]['childids'] = implode(",", $childid);
                }
            }
            $category_data = array("code" => 0, "msg" => "", "count" => $count, "data" => $category);
            $category_json = json_encode($category_data);
            echo $category_json;
            exit;
        }
        if ($_GPC['api'] == 'del' && $_GPC['cid']) {

            $childid = pdo_getcolumn("xn_erp_category", array('id' => $_GPC['cid']), 'childid');
            $childid_arr = unserialize($childid);
            if (is_array($childid_arr)) {
                foreach ($childid_arr as $key => $value) {
                    pdo_delete("xn_erp_category", array('id' => $value));
                }
            }

            $result = pdo_delete('xn_erp_category', array('id' => $_GPC['cid']));
            if (!empty($result)) {
                echo "删除成功";
            } else {
                echo "删除失败";
            }
            exit;
        }
        if ($_GPC['api'] == 'edit' && $_GPC['name']) {

            $result = pdo_update('xn_erp_category', array('name' => $_GPC['name'], 'sort' => $_GPC['sort']), array('id' => $_GPC['cid']));
            if (!empty($result)) {
                echo "修改成功";
            } else {
                echo "修改失败";
            }

            exit;
        }

        if ($_GPC['api'] == 'add' && $_GPC['name']) {
            $pchildid_list = pdo_getall("xn_erp_category", array('pid' => $_GPC['pid']), 'id');
            foreach ($pchildid_list as $key => $value) {
                $pchildid_list_arr[] = $value['id'];
            }
            $result = pdo_insert('xn_erp_category', array('name' => $_GPC['name'], 'sort' => $_GPC['sort'], 'pid' => $_GPC['pid'], 'level' => $_GPC['level'], 'type' => $type));
            $id = pdo_insertid();
            $pchildid_list_arr[] = $id;
            $josn = json_encode($pchildid_list_arr);
            $presult = pdo_update("xn_erp_category", array("childid" => serialize($pchildid_list_arr)), array('id' => $_GPC['pid']));
            if (!empty($result)) {
                echo "添加成功".$type;
            } else {
                echo "添加失败";
            }
            exit;
        }

        if ($_GPC['api'] == 'tree') {
            $categoryarr = pdo_getall("xn_erp_category",array('type'=>$type));
    
            echo json_encode(list_to_tree($categoryarr));
            
            exit;
        }
        echo '接口错误';
        exit;
    }

    include $this->template('web/set/set_category_' . $_GPC['type']);
}
